import 'package:cbec_app/components/base/base_interactive.dart';
import 'package:cbec_app/components/common/ad_count_down.dart';
import 'package:cbec_app/pages/launch/splash_controller.dart';
import 'package:cbec_app/routes/routes.dart';
import 'package:cbec_app/utils/ImageHelper.dart';
import 'package:cbec_app/utils/Tools.dart';
import 'package:flutter/cupertino.dart';
import 'package:get/get.dart';

///开屏广告
class SplashPage extends GetView<SplashController> {
  const SplashPage({super.key});

  @override
  Widget build(BuildContext context) {
    return GetBuilder<SplashController>(
      builder: (controller) {
        return Stack(
          alignment: AlignmentDirectional.bottomEnd,
          children: [
            getAdType(),
            Positioned(
              right: 20,
              top: 50,
              child: AdCountdown(
                countdownSeconds: controller.config['waitTime'] ?? 5,
                finished: (byUserClick) {
                  Get.offAllNamed(Routes.main);
                },
              ),
            ),
          ],
        );
      },
    );
  }

  ///获取广告类型
  Widget getAdType() {
    switch (controller.config['type']) {
      case 2: //视频广告
      case 1: //图片广告
      default:
        return BaseInteractive(
          childWidget: Container(
            decoration: BoxDecoration(image: DecorationImage(image: ImageHelper.imgProvider(controller.config['fileUrl'] ?? 'splash.png'), fit: BoxFit.cover)),
          ),
          onClick: () {
            jumpPage(controller.config);
          },
        );
    }
  }

  ///跳转指定页面
  void jumpPage(Map<String, dynamic> config) {
    switch (config['jumpType']) {
      case 1: //有跳转
        Get.offAllNamed(Routes.main);
        Future.delayed(const Duration(seconds: 1), () {
          Tools.jumpPage(config['content']);
        });

        break;
      case 2: //热区跳转
        Get.offAllNamed(Routes.main);
        Future.delayed(const Duration(seconds: 1), () {
          Tools.jumpPage(controller.config['content']);
        });
        break;
      case 0: //无跳转
        break;
      default:
        break;
    }
  }
}
